package uit.shoeshop.action;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import uit.shoeshop.dto.BillDetailDTO;
import uit.shoeshop.dto.BillInfoDTO;
import uit.shoeshop.lib.Constants;
import uit.shoeshop.lib.Util;
import uit.shoeshop.model.PaymentModel;

import com.opensymphony.xwork2.ActionSupport;
public class ManagebillAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private String link;
	private String sid;
	private String saction;
	private ArrayList<BillInfoDTO> billList;
	private ArrayList<BillDetailDTO> billDetailList;
	private BillInfoDTO oneBill;
	private int accepted;
	private int total;
	private String pagination;
	private int page;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	public String execute() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = NONE;

    	PaymentModel billinfo = new PaymentModel();
    	setLink(this.request.getRequestURL().toString());
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null && getSid() !=null){
        		if(getSaction().equals("del") && Util.isIntNumber(getSid())){
        			int result = billinfo.delBill(Integer.parseInt(getSid()));
        			if( result == 1)
        				ret = SUCCESS;
        			else
        				ret = ERROR;
        		}else{
        			ret= ERROR;
        		}
        	}else{     		
        		setBillList(billinfo.getAllBill(getPage()==0?1:getPage()));
        		setPagination(Util.Panigation("/shoeshop/managebill.html?page=", billinfo.countTotalBill(), Constants.LIMIT_BILL, getPage()==0?1:getPage()));
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		billinfo = null;
    	}     	
    	return ret;
    }

    public String view() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = ERROR;
    	setLink("managebill.html");
    	PaymentModel billinfo = new PaymentModel();
    	
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null && getSid() !=null){
        		if(getSaction().equals("view") && Util.isIntNumber(getSid())){
        			setOneBill(billinfo.getBillbyID(Integer.parseInt(getSid())));
        			setBillDetailList(billinfo.getAllBillDetail(Integer.parseInt(getSid())));
        			setTotal(billinfo.getTotalPrice(getBillDetailList()));
        			if(getOneBill() != null && getOneBill().getFull_name()!= null)
        				ret= NONE;
        			else
        				ret=ERROR;
        		}else if(getSaction().equals("finish") && Util.isIntNumber(getSid())){
        			int result = billinfo.editBill(Integer.parseInt(getSid()),2);
        			if( result == 1)
        				ret = SUCCESS;
        			else
        				ret = ERROR;
        		}else if(getSaction().equals("cancel") && Util.isIntNumber(getSid())){
        			int result = billinfo.editBill(Integer.parseInt(getSid()),3);
        			if( result == 1)
        				ret = SUCCESS;
        			else
        				ret = ERROR;
        		}else{
        			ret = ERROR;
        		}
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		billinfo = null;
    	}     	
    	return ret;
    }

	public void setLink(String link) {
		this.link = link;
	}

	public String getLink() {
		return link;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSid() {
		return sid;
	}

	public void setSaction(String saction) {
		this.saction = saction;
	}

	public String getSaction() {
		return saction;
	}

	public void setBillList(ArrayList<BillInfoDTO> billList) {
		this.billList = billList;
	}

	public ArrayList<BillInfoDTO> getBillList() {
		return billList;
	}

	public void setBillDetailList(ArrayList<BillDetailDTO> billDetailList) {
		this.billDetailList = billDetailList;
	}

	public ArrayList<BillDetailDTO> getBillDetailList() {
		return billDetailList;
	}

	public void setOneBill(BillInfoDTO oneBill) {
		this.oneBill = oneBill;
	}

	public BillInfoDTO getOneBill() {
		return oneBill;
	}

	public void setAccepted(int accepted) {
		this.accepted = accepted;
	}

	public int getAccepted() {
		return accepted;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getTotal() {
		return total;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}    

}
